package ai.people.netmon.userconfig;

import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 说明
 *
 * @author zhenglin
 * @since 2022/3/25
 */
@Slf4j
public class Handle {

    private static final Pattern FACEBOOK_PATTERN = Pattern.compile("https?://www\\.facebook\\.com/(profile.php\\?id=)?(.+)(&)?");
    private static final Pattern PATTERN2 = Pattern.compile("(.+)[&/]");
    private static final Pattern TWITTER_PATTERN = Pattern.compile("https?://(www\\.)?twitter\\.com/(.+)/?");
    private static final Pattern YOUTUBE_PATTERN = Pattern.compile("https?://www\\.youtube\\.com/(channel)?(c)?(user)?/?(.+)/");
    private static final Pattern REDDIT_PATTERN = Pattern.compile("https?://www\\.reddit\\.com/(r)?(user)?/?(.+)/");

    public static void main(String[] args) {
        {
            List<String> list = Arrays.asList("https://www.youtube.com/channel/UCZ4NwvuGYgFyjnRRJekdnHw/videos",
                    "https://www.youtube.com/c/UCZ4NwvuGYgFyjnRRJekdnHw/videos",
                    "https://www.youtube.com/user/UCZ4NwvuGYgFyjnRRJekdnHw/videos",
                    "https://www.youtube.com/UCZ4NwvuGYgFyjnRRJekdnHw/videos",
                    "http://www.youtube.com/UCZ4NwvuGYgFyjnRRJekdnHw/videos"
            );

            for (String link : list) {
                log.info("-----------------------------");
                log.info("原链接:{}",link);
                Pattern compile = Pattern.compile("https?://www\\.youtube\\.com/(channel)?(c)?(user)?/?(.+)/");
                Matcher matcher = compile.matcher(link);
                if (matcher.find()) {
                    log.info("匹配:{}",matcher.group(4));
                }
                log.info("handleUrl:{}",handleUrl(link,AnalysisSourceEnum.youtube.name()));
            }
        }
        {
            List<String> list = Arrays.asList("https://www.facebook.com/xxxxx",
                    "https://www.facebook.com/xxxxx/",
                    "https://www.facebook.com/profile.php?id=xxxxx&sk=friends",
                    "https://www.facebook.com/profile.php?id=xxxxx&sk=fol",
                    "https://www.facebook.com/xxxxx/following"
            );

            for (String link : list) {
                log.info("-----------------------------");
                log.info("原链接:{}",link);
                Pattern compile = Pattern.compile("https?://www\\.facebook\\.com/(profile.php\\?id=)?(.+)(&)?");
                Matcher matcher = compile.matcher(link);
                if (matcher.find()) {
                    String group = matcher.group(2);
                    log.info("匹配:{}", group);
                    if (group.contains("/") || group.contains("&")){
                        Pattern compile2 = Pattern.compile("(.+)[&/]");
                        Matcher matcher1 = compile2.matcher(group);
                        if (matcher1.find()) {
                            System.out.println(matcher1.group(1));
                        }
                    }
                }
                log.info("handleUrl:{}",handleUrl(link,AnalysisSourceEnum.facebook.name()));
            }
        }
        {
            List<String> list = Arrays.asList("https://twitter.com/0000000000Nikki",
                    "https://twitter.com/0000000000Nikki/"
            );

            for (String link : list) {
                log.info("-----------------------------");
                log.info("原链接:{}",link);
                Pattern compile = Pattern.compile("https?://(www\\.)?twitter\\.com/(.+)/?");
                Matcher matcher = compile.matcher(link);
                if (matcher.find()) {
                    String group = matcher.group(2);
                    log.info("匹配:{}", group);
                }
                log.info("handleUrl:{}",handleUrl(link,AnalysisSourceEnum.twitter.name()));
            }
        }
        {
            List<String> list = Arrays.asList("https://www.reddit.com/user/ElementorOfficial/",
                    "https://www.reddit.com/r/NoStupidQuestions/"
            );

            for (String link : list) {
                log.info("-----------------------------");
                log.info("原链接:{}",link);
                log.info("handleUrl:{}",handleUrl(link,AnalysisSourceEnum.reddit.name()));
            }
        }
    }

    private static String handleUrl(String text,String infoType) {

        Pattern pattern = null;
        int groupIndex = 0;
        if (AnalysisSourceEnum.facebook.name().equals(infoType)) {
            pattern = FACEBOOK_PATTERN;
            groupIndex = 2;
        } else if (AnalysisSourceEnum.twitter.name().equals(infoType)) {
            pattern = TWITTER_PATTERN;
            groupIndex = 2;
        } else if (AnalysisSourceEnum.youtube.name().equals(infoType)) {
            pattern = YOUTUBE_PATTERN;
            groupIndex = 4;
        }else if (AnalysisSourceEnum.reddit.name().equals(infoType)) {
            pattern = REDDIT_PATTERN;
            groupIndex = 3;
        }
        if (pattern == null) {
            return null;
        }
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            String group = matcher.group(groupIndex);
            if (group.contains("/") || group.contains("&")){
                pattern = PATTERN2;
                Matcher matcher1 = pattern.matcher(group);
                if (matcher1.find()){
                    return matcher1.group(1);
                }
            }
            return group;
        }
        return null;
    }
}
